package com.File;

import org.junit.Test;

import java.io.*;

/**
 * 处理流之一: 缓冲流的作用
 *
 *
 * 1.缓冲流:
 *
 * BufferedInputStream
 * BufferedOutputStream
 * BufferedReader
 * BufferedWriter
 *
 *
 * 2.作用: 提高流的读取，写入的速度
 *  提高读写速度的原因; 内部提供了一个缓冲区
 *
 *
 *  3.处理流,就是套接在已有的流的基础上。
 *
 */
public class BufferedTest {

    /**
     * 实现非文本文件的复制
     */

    @Test
    public void BufferedStreamTest() throws IOException {
        //1.造文件
        File srcFile = new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian.PNG");
        File destFile = new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian.PNG");

        //2.造节点流
        FileInputStream fileInputStream = new FileInputStream(srcFile);
        FileOutputStream fileOutputStream = new FileOutputStream(destFile);

        //3.造缓冲流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        //3.复制的细节:读取，写入
        byte[] buffer = new byte[10];
        int len;
        while ((len = bufferedInputStream.read(buffer)) != -1){
            bufferedOutputStream.write(buffer, 0, len);
        }


        //4.资源关闭
        //要求:先关闭外层的流，再关闭内层的流
        bufferedInputStream.close();
        bufferedOutputStream.close();

//        说明:关闭外层流的同时，内层流也会自动关闭，关于内层流的关闭，我们可以省略。
//        fileInputStream.close();
//        fileOutputStream.close();
    }

    //实现文件复制的方法
    public void copyFileWithBufferd(String srcPath, String destPath) throws IOException {

        //1.造文件
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        //2.造节点流
        FileInputStream fileInputStream = new FileInputStream(srcFile);
        FileOutputStream fileOutputStream = new FileOutputStream(destFile);

        //3.造缓冲流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        //3.复制的细节:读取，写入
        byte[] buffer = new byte[10];
        int len;
        while ((len = bufferedInputStream.read(buffer)) != -1){
            bufferedOutputStream.write(buffer, 0, len);

            //刷新缓冲区
//            bufferedOutputStream.flush();
        }


        //4.资源关闭
        //要求:先关闭外层的流，再关闭内层的流
        bufferedInputStream.close();
        bufferedOutputStream.close();

//        说明:关闭外层流的同时，内层流也会自动关闭，关于内层流的关闭，我们可以省略。
//        fileInputStream.close();
//        fileOutputStream.close();


    }



    @Test
    public void testCopyFileWithBuffered() throws IOException {

        long start = System.currentTimeMillis();

        String srcPath = "D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian.PNG";
        String destPath = "D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupia6.PNG";


        copyFileWithBufferd(srcPath, destPath);


        long end = System.currentTimeMillis();


        System.out.println("复制操作时间为:" + (end - start));


    }


    /**
     * 使用BufferReader和BufferWriter实现文本文件的复制
     */
    @Test
    public void testBufferedReaderBufferedWriter() throws IOException {
        //1.造文件
        File srcFile = new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian.PNG");
        File destFile = new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian.PNG");


        //2.1 造字符流
        FileReader fileReader = new FileReader(srcFile);
        FileWriter fileWriter = new FileWriter(destFile);



        //2.2开启缓冲流
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


        //3.复制的细节
//        方式一:char[]数组
//        char[] bufer = new char[1024];
//        int len;
//        while ((len = bufferedReader.read(bufer)) != -1){
//            bufferedWriter.write(bufer, 0, len);
//        }

        //方式二:String
        //一次读取一行
        String data;
        while ((data = bufferedReader.readLine()) != null){
//    方式一：        bufferedWriter.write(data + "\n"); //data中是不包含换行符，想要输出的文件包含换行，需要自己添加。
//        方法二:
            bufferedWriter.write(data);
            bufferedWriter.newLine(); //提供换行的操作
        }



        //4.关闭资源
        bufferedReader.close();
        bufferedWriter.close();

    }
}
